<?php

/**
 * manage blog with comment
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class BusTripController extends Controller {

    private $_model;
    public $activeMenu = 'bus';

    public function actionIndex($id) {
        
        $bus = $this->loadModelBus($id);
        $models = new BusTrip();
        
        $models->unsetAttributes();  // clear any default values
        if (isset($_GET['BusTrip']))
            $models->attributes = $_GET['BusTrip'];

        $this->render('index', array(
            'models' => $models,
            'bus' => $bus,
        ));
    }

    public function actionCreate($id) {
        $model = new BusTrip();
        
        $bus = $this->loadModelBus($id);
        
        if (isset($_POST['BusTrip'])) {
            $iStartTime = $_POST['BusTrip']['start_time']['hour'] * 60 * 60 + $_POST['BusTrip']['end_time']['minute'] * 60;
            $iEndTime = $_POST['BusTrip']['end_time']['hour'] * 60 * 60 + $_POST['BusTrip']['end_time']['minute'] * 60;
            $model->attributes = array(
                'start_time' => $iStartTime,
                'end_time' => $iEndTime,
                'ordering' => $_POST['BusTrip']['ordering'],
                'bus_detail_id' => $_POST['BusTrip']['bus_detail_id'],
            );

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'BusTrip successfully added');
                $this->redirect(array('/bustrip/index', 'id'=> $bus->bus_detail_id));
            }
        }
        
        //get latest order: 
        $modelBusTripLatest = BusTrip::model()->find(array('condition' => 'bus_detail_id = ' . (int) $id, 'order' => 'ordering DESC'));
        $iLatestOrdering = 0;
        if($modelBusTripLatest) {
            $iLatestOrdering = $modelBusTripLatest->ordering;
        }
        $model->ordering = ++$iLatestOrdering;

        $this->render('create', compact('model', 'bus'));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel();
        
        $bus = $this->loadModelBus($model->bus_detail_id);
        
        if (isset($_POST['BusTrip'])) {
            $iStartTime = HCTime::timeToSeconds($_POST['BusTrip']['start_time']['hour'], $_POST['BusTrip']['start_time']['minute']);
            $iEndTime = HCTime::timeToSeconds($_POST['BusTrip']['end_time']['hour'], $_POST['BusTrip']['end_time']['minute']);
            
            $model->attributes = array(
                'start_time' => $iStartTime,
                'end_time' => $iEndTime,
                'ordering' => $_POST['BusTrip']['ordering'],
                'bus_detail_id' => $_POST['BusTrip']['bus_detail_id'],
            );

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'BusTrip successfully updated');
                $this->redirect(array('/bustrip/index', 'id'=> $bus->bus_detail_id));
            }
        }
        
        

        $this->render('create', compact('model', 'bus'));
    }

    public function actionDelete($id) {

        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request

            $this->loadModel()->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if (!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }

        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }
    

    public function loadModel() {

        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                $this->_model = BusTrip::model()->findByPk($_GET['id']);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }
    
    public function loadModelBus($id = NULL) {

            if (isset($id)) {

                $model = BusDetail::model()->with(array('bus'))->findByPk($id);
            }

            if ($model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
            
            return $model;
    }

}